﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Licogi10.Entities;
using Licogi10.Data;
using System.Web.Security;

public partial class Admin_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session.Clear();
        FormsAuthentication.SignOut();
    }

    protected void cmdDangNhap_Click(object sender, ImageClickEventArgs e)
    {
        string strUserName = Common.StandardString(UserName.Text);
        string strPassWord = Common.StandardString(Password.Text);
        string strUserCode = strUserName + strPassWord;
        strPassWord = Common.EncryptMD5(strPassWord);
        strUserCode = Common.EncryptMD5(strUserCode);
        DoLogin(strUserName, strPassWord, strUserCode);
    }

    private void DoLogin(string strUserName, string strPassword, string strUserCode)
    {
        NguoiDung ent_Nguoidung = DataRepository.NguoiDungProvider.GetByTenTruyCap(strUserName);
        if (ent_Nguoidung == null)
        {
            WebMessage.Show("Người dùng không tồn tại trong hệ thống!");
            UserName.Focus();
            return;
        }
        string CurrentPassword = ent_Nguoidung.MatKhau;
        string CurrentUserCode = ent_Nguoidung.MaTruyCap;
        string strPassWord = Common.StandardString(Password.Text);
        if (strPassword.Equals(CurrentPassword) && strUserCode.Equals(CurrentUserCode))
        {
            string strUserInfor = string.Format("{0};{1};{2}", ent_Nguoidung.Id, ent_Nguoidung.TenTruyCap, ent_Nguoidung.HoTen);
            FormsAuthentication.SetAuthCookie(strUserInfor, false);
            Response.Redirect("~/Admin/HomeAdmin.aspx");
        }
        else
        {
            WebMessage.Show("Mật khẩu nhập vào không đúng");
            Password.Focus();
            return;
        }
    }
    
}